package com.huzz.util.encryption;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

public class RSAUtils{


    /**
     * 加密
     * @param content
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static String encryption(String content,String publicKey) {
        try {
            byte[] decodeBase64 = Base64.decodeBase64(publicKey);
            PublicKey rsa = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decodeBase64));
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE,rsa);
            String outStr  = Base64.encodeBase64String(cipher.doFinal(content.getBytes()));
            return outStr;
        }catch (Exception e){
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 解密
     * @param encryption
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String decryption(String encryption, String privateKey)throws Exception{
        byte[] encryptionByte = Base64.decodeBase64(encryption.getBytes("UTF-8"));
        byte[] privateKeyByte = Base64.decodeBase64(privateKey.getBytes("UTF-8"));
        RSAPrivateKey generatePublic = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyByte));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, generatePublic);
        return new String(cipher.doFinal(encryptionByte));
    }

    /**
     * 验证
     * @param data
     * @param encryption
     * @param privateKey
     * @return
     */
    public static boolean verification(String data, String encryption, String privateKey) {
        try {
            if (decryption(encryption, privateKey).equals(data)) {
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    /**
     * 随机生成密钥对
     *
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static Map<String, String> genKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(512);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        //私钥
        PrivateKey privateK = keyPair.getPrivate();
        //公钥
        PublicKey publicK = keyPair.getPublic();

        //PrivateKey2String
        String privateKey = new String(Base64.encodeBase64(privateK.getEncoded()));
        //PublicKey2String
        String publicKey = new String(Base64.encodeBase64(publicK.getEncoded()));
        System.out.println("公钥："+publicKey);
        System.out.println("私钥："+privateKey);
        Map<String, String> map = new HashMap<>();
        map.put("publicKey",publicKey);
        map.put("privateKey",privateKey);
        return map;
    }
}
